<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Stage as StageModel;
use fast\Tree;
use think\Db;
use think\Session;

/**
 * 分类管理
 *
 * @icon   fa fa-list
 * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Professor extends Backend
{

    /**
     * @var \app\common\model\Category
     */
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];
    protected $mainhost = "";

    public function _initialize()
    {
        parent::_initialize();
        $this->mainhost = "http://".$_SERVER["HTTP_HOST"];
        $this->model = model('app\common\model\Professor');

        $this->categorylist =   $this->model->order("id desc")->select();
        $typeList = StageModel::getTypeList();
        $this->assignconfig('typeList', $typeList);
        //关联项目
        $prolist = db("project")->field("id,title")->select();
        $this->view->assign("prolist", $prolist);

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $search = $this->request->request("search");
            $type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];

            foreach ($this->categorylist as $k => $v) {
                $v['imgs'] = $this->mainhost.$v['imgs'];
                $v['intro'] = subText3( $v['intro'],40);
                $v['goodat'] = subText3( $v['goodat'],40);
                $v['field'] = subText3( $v['field'],40);
                $v['education'] = subText3( $v['education'],40);
                $v['honour'] = subText3( $v['honour'],40);



                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $this->categorylist;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->categorylist;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
                }
            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $model =  $this->model;
            $params = $this->request->post();

            if ($params) {
                $params = $this->preExcludeFields($params);


                if(!empty($params["row"]["imgs"])){
                    $imgs = explode(",",$params["row"]["imgs"]);
                    $params["row"]["img"] = $imgs[0];
                }

                //关联项目id
                $proids = $params['proids'];
                if($proids[0] != ""){
                   $pids = implode(",",$proids);
                    $params["row"]["proids"] = $pids;
                }

                //替换富文本图片未绝对路径
                if($params["row"]['intro']){
                    $fwbcontent =$params["row"]['intro'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['intro'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['goodat']){
                    $fwbcontent =$params["row"]['goodat'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['goodat'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['jigou']){
                    $fwbcontent =$params["row"]['jigou'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['jigou'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['field']){
                    $fwbcontent =$params["row"]['field'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['field'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['education']){
                    $fwbcontent =$params["row"]['education'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['education'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['honour']){
                    $fwbcontent =$params["row"]['honour'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['honour'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['researcher']){
                    $fwbcontent =$params["row"]['researcher'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['researcher'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['project']){
                    $fwbcontent =$params["row"]['project'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['project'] = $fwbcontent2;//带有绝对路径
                }
                //替换富文本图片未绝对路径
                if($params["row"]['content']){
                    $fwbcontent =$params["row"]['content'];
                    $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                    $params["row"]['content'] = $fwbcontent2;//带有绝对路径
                }



                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }

                $result = false;
                Db::startTrans();

                try {

                    $result = $model->save($params["row"]);


                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $proids = $this->request->post("proids/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                try {

                    if(!empty($params["imgs"])){
                        $imgs = explode(",",$params["imgs"]);
                        $params["img"] = $imgs[0];
                    }else{
                        $params["img"] = "";
                    }

                    //关联项目id
                    if($proids[0] != ""){
                        $pids = implode(",",$proids);
                        $params["proids"] = $pids;
                    }

                    //替换富文本图片未绝对路径
                    if($params['intro']){
                        $fwbcontent =$params['intro'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['intro'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['goodat']){
                        $fwbcontent =$params['goodat'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['goodat'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['jigou']){
                        $fwbcontent =$params['jigou'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['jigou'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['field']){
                        $fwbcontent =$params['field'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['field'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['education']){
                        $fwbcontent =$params['education'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['education'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['honour']){
                        $fwbcontent =$params['honour'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['honour'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['researcher']){
                        $fwbcontent =$params['researcher'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['researcher'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['project']){
                        $fwbcontent =$params['project'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['project'] = $fwbcontent2;//带有绝对路径
                    }
                    //替换富文本图片未绝对路径
                    if($params['content']){
                        $fwbcontent =$params['content'];
                        $fwbcontent2 = pregReplaceImg2($fwbcontent,$this->mainhost);
                        $params['content'] = $fwbcontent2;//带有绝对路径
                    }


                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
}
